<?php
/**
 * Created by YUNYI.
 * User: james.xiao
 * Date: 2021/6/5
 * Time: 16:21
 */

namespace app\model;


use think\facade\Cache;
use think\facade\Db;

class CGQData
{
    private $record = [
        "fengSu",
        "fengXiang",
        "kongQiShiDu",
        "kongQiWenDu",
        "tuRangDianDao",
        "tuRangShiDu",
        "tuRangWenDu",
        "zaoSheng",
    ];
    private $_01arr = [
        "fengSu",
        "fengXiang",
        "kongQiShiDu",
        "kongQiWenDu",
        "kongQiWenDu",
        "tuRangShiDu",
        "tuRangWenDu",
        "zaoSheng",

    ];

    public static function init()
    {
        return new self();
    }

    public function doData($str)
    {
        $pattern = "/;(YY_.*?),/";
        preg_match_all($pattern , $str , $res);
        echo date("Y-m-d H:i:s"),'|',join(";" , $res[1] ). "\n";
        array_map(function ($val) use (&$list){
            list($k , $v) = explode("=" , $val);
            $k = str_replace(['YY_' , '-Rtd'] , '' , $k);
            $list[$k] = (int) $v;
        } , $res[1]);
        $insert = [
            'device_id' => 1 ,
            'date' => date("Y-m-d"),
            'datetime' => date("Y-m-d H:i:s"),
            'time' => time(),
            'create_time' => time()
        ];
        $data = [];
        foreach ($this->record as $v) {
            if (empty($list[$v]))continue;
            if (Cache::has($v) && Cache::get($v) == $list[$v])continue;
            Cache::set($v , $list[$v]);
            $val = $list[$v];
            if (in_array($v , $this->_01arr)){
                $val *= 0.1;
            }
            $data[] = [
                'type' => $v ,
                'value' => $val,
                'fu_value' => - $val,
            ] + $insert;

        }

        $guangZhao = hexdec(dechex($list['guangZhaoA']) . dechex($list['guangZhaoB']));
        $data[] = [
            'type' => 'guangZhaoDu' ,
            'value' => $guangZhao,
            'fu_value' => - $guangZhao,
        ] + $insert;
        Db::table('cgq_data')->insertAll($data);

    }

}